package company

import (
	"energy-amiot-platform/core"
	"energy-amiot-platform/model/dbspo"
	"energy-amiot-platform/model/request"
	"energy-amiot-platform/model/response"
	"energy-amiot-platform/repository"
	"energy-amiot-platform/utils/respout"
	"errors"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"gorm.io/gorm"
)

var (
	moduleCode = 3
)

// @Tags 企业管理
// @Summary 添加一个企业
// @Description 添加一个企业
// @Accept  json
// @Product json
// @Param   input body   request.CompanyAddReq  true		"企业添加数据"
// @Success 200 {object} response.Response "Ok"
// @Router /company [post]
func create(ctx *gin.Context) {
	var handleCode = 1
	var req request.CompanyAddReq
	err := ctx.ShouldBindJSON(&req)
	if err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 0, respout.StrErrParse, err, nil)
		return
	}

	var po dbspo.Company
	err = copier.Copy(&po, &req)
	if err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 1, respout.StrFail, err, nil)
		return
	}

	db := core.DATABASE.Cur()
	db.Transaction(func(tx *gorm.DB) error {
		t, b := repository.COMPANY.ExistWithName(tx, po.Name)
		switch {
		case t.Error != nil:
			respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 2, respout.StrFail, t.Error, nil)
			return t.Error
		case b == true:
			respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 3, respout.StrFail, errors.New("企业已存在"), nil)
			return t.Error
		}

		t = repository.COMPANY.Create(t, &po)
		if t.Error != nil {
			respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 4, respout.StrFail, t.Error, nil)
			return t.Error
		}

		respout.Ok(ctx, &po)
		return nil
	})
}

// @Tags 企业管理
// @Summary 添加一个企业
// @Description 添加一个企业
// @Product json
// @Param   name query string true			"企业名称"
// @Param   pageSize query int false		"每页数量" 	default(10)
// @Param   pageNum query int false			"第几页"		default(1)
// @Success 200 {object} response.Response{result=response.PageData{list=dbspo.Company}} "Ok"
// @Router /company [get]
func pagingQuery(ctx *gin.Context) {
	var handleCode = 2
	var req request.CompanyPagingQueryReq
	err := ctx.ShouldBindQuery(&req)
	if err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 0, respout.StrErrParse, err, nil)
		return
	}
	if req.PageSize == 0 {
		req.PageSize = 10
	}
	if req.PageNum == 0 {
		req.PageNum = 1
	}

	var count int64
	var result []dbspo.Company
	db := core.DATABASE.Cur()
	if err = db.Model(&dbspo.Company{}).Where("name like ?", "%"+req.Name+"%").Count(&count).Error; err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 1, respout.StrFail, err, nil)
		return
	}

	offset := req.PageSize * (req.PageNum - 1)
	err = db.Model(&dbspo.Company{}).Where("name like ?", "%"+req.Name+"%").Offset(offset).Limit(req.PageSize).Scan(&result).Error
	if err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 2, respout.StrFail, err, nil)
		return
	}

	respout.Ok(ctx, response.PageData{
		Total: count,
		List:  result,
	})
}

// @Tags 企业管理
// @Summary 获取指定标识的企业
// @Description 获取指定标识的企业
// @Product json
// @Param   id query int true		"企业标识"
// @Success 200 {object} response.Response{result=dbspo.Company} "Ok"
// @Router /company/{id} [get]
func findByKey(ctx *gin.Context) {
	var handleCode = 3
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 0, respout.StrErrParse, err, nil)
		return
	}

	var result dbspo.Company
	err = core.DATABASE.Cur().First(&result, "id=?", id).Error
	switch {
	case err == gorm.ErrRecordNotFound:
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 1, respout.StrFail, errors.New("企业不存在"), nil)
		return
	case err != nil:
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 2, respout.StrFail, err, nil)
		return
	}

	respout.Ok(ctx, result)
}

// @Tags 企业管理
// @Summary 删除企业
// @Description 删除企业
// @Product json
// @Param 	id 		path 	int 	true 	"企业标识"
// @Success 200 {object} response.Response "Ok"
// @Router /company/{id} [delete]
func deleteByID(ctx *gin.Context) {
	var handleCode = 4
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64)
	if err != nil {
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 0, respout.StrErrParse, err, nil)
		return
	}

	db := core.DATABASE.Cur()
	tx, c := repository.PRODUCT.CountByCid(db, id)
	switch {
	case tx.Error != nil:
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 1, respout.StrFail, tx.Error, nil)
		return
	case *c > 0:
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 2, respout.StrFail, errors.New("该企业有产品"), nil)
		return
	}

	tx = repository.COMPANY.DeleteById(db, id)
	switch {
	case tx.Error != nil:
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 3, respout.StrFail, tx.Error, nil)
		return
	case tx.RowsAffected == 0:
		respout.SendError(ctx, core.APP.Code, moduleCode, handleCode, 4, respout.StrFail, tx.Error, nil)
		return
	}

	respout.Ok(ctx, nil)
}
